package game;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;
import gfx.Camera;
import gfx.Glyphs;
import gfx.Image;
import gfx.Shapes;
import util.GameClock;
import util.GameVars;
import util.InteractableInterface;
import util.Logger;
import util.VertexBuffer;

public class SplashScreen implements InteractableInterface{
	Image splashImage;

	private String txt;
	
	private short week, drawIterator;
	
	private boolean next;
	private boolean isLoaded = false;
//	private boolean isReady;

	// Stolen for Don't let up, but can be used for any animated splash, I'm sure.
	float rotation;
	// Don't Let Up Stuff
	float[] squareone = {.15f, .1f, 0, .2f, .1f, 0, .15f, .2f, 0, .2f, .2f, 0};
	float[] squaretwo = {-.2f, .1f, 0, -.15f, .1f, 0, -.2f, .2f, 0, -.15f, .2f, 0};
	float[] squarethree = {.15f, -.2f, 0, .2f, -.2f, 0, .15f, -.1f, 0, .2f, -.1f, 0};
	float[] squarefour = {-.2f, -.2f, 0, -.15f, -.2f, 0, -.2f, -.1f, 0, -.15f, -.1f, 0};
	
	float[] squarefive = {.1f, .15f, 0, .2f, .15f, 0, .1f, .2f, 0, .2f, .2f, 0};
	float[] squaresix = {.1f, -.2f, 0, .2f, -.2f, 0, .1f, -.15f, 0, .2f, -.15f, 0};
	float[] squareseven = {-.2f, .15f, 0, -.1f, .15f, 0, -.2f, .2f, 0, -.1f, .2f, 0};
	float[] squareeight = {-.2f, -.2f, 0, -.1f, -.2f, 0, -.2f, -.15f, 0, -.1f, -.15f, 0};
	
	public SplashScreen(){
		week = 5;
		load();
	}
	
	public void load(){
		next = false;
//		splashImage = new Image(GameVars.TEXTURE_SPLASH_BACKGROUND, Camera.GL_WIDTH, Camera.GL_HEIGHT);
//		isLoaded = true;
		switch(week){
		case 4: // N-Puzzle +
			txt = "N-Puzzle+";
			break;
		}
	}
	
	public void update(long time){
		switch(week){
		case 2: // Don't Let Up
			rotation += (180 * time) / 1000f; 
		}
	}
	
	// TODO Figure out why this is the only fix :x
	public void draw(GL10 gl){
		switch(week){
		case 0: // Master Build
			break;
		case 1: // Chain Memory
			Shapes.drawSquare(gl, 5, 3, 5, 3f, .5f, .5f, .5f, 1);
			Shapes.drawSquare(gl, 5, 3, 2.55f, .55f, 0, 0, 0, 1);
			Shapes.drawSquare(gl, 3, 3, .45f, .45f, .2073f, .7345f, .5183f, 1);
			Shapes.drawSquare(gl, 4, 3, .45f, .45f, .9052f, .2395f, .4583f, 1);
			Shapes.drawSquare(gl, 5, 3, .45f, .45f, .2745f, .2452f, .5984f, 1);
			Shapes.drawSquare(gl, 6, 3, .45f, .45f, .1822f, .2014f, .5770f, 1);
			Shapes.drawSquare(gl, 7, 3, .45f, .45f, .7529f, .3334f, .0548f, 1);
			gl.glPushMatrix(); 
			gl.glTranslatef(5, 3, 0);
			gl.glScalef(2, 2, 0);
			Glyphs.drawString(gl, "Chain Memory", null, GameVars.TEXT_ALIGNMENT_CENTER);
			gl.glPopMatrix();
			break;
		case 2: // Don't Let Up
			gl.glPushMatrix();
			gl.glTranslatef(5, 3, 0);
			gl.glPushMatrix();
			gl.glScalef(2, 2, 0);
			gl.glRotatef(rotation, 0, 0, 1);
			gl.glDisable(GL10.GL_TEXTURE_2D);
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glFrontFace(GL10.GL_CCW);
			gl.glColor4f(1, 0, 0, 1);
			drawBits(gl);
			gl.glRotatef(45, 0, 0, 1);
			drawBits(gl);
			
			gl.glColor4f(1, 1, 1, 1);
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glPopMatrix();

			Glyphs.drawString(gl, "Don't Let Up!", null, GameVars.TEXT_ALIGNMENT_CENTER);
			gl.glPopMatrix();
			
			break;
		case 3: // Squishy
			gl.glPushMatrix();
			gl.glTranslatef(5, 3, 0);
			Glyphs.drawString(gl, "Squishy", null, GameVars.TEXT_ALIGNMENT_CENTER);
			gl.glPopMatrix();
			Shapes.drawSquare(gl, 5, 1.25f, .25f, .25f, 0, 1, 0, 1);
			break;
		case 4: // Eight Puzzle Plus
			Shapes.drawSquare(gl, 5, 3, 3, 3, 1, 1, 1, 1);
			for(drawIterator = 0; drawIterator < 8; ++ drawIterator){
				Shapes.drawSquare(gl, 3 + drawIterator % 3 * 2, 5 - drawIterator / 3 * 2, .95f, .95f, 0, 0, 0, 1);
				gl.glPushMatrix();
				gl.glTranslatef(3 + drawIterator % 3 * 2, 5 - drawIterator / 3 * 2, 0);
				gl.glScalef(2, 2, 1);
				Glyphs.drawString(gl, "" + txt.charAt(drawIterator), null, GameVars.TEXT_ALIGNMENT_CENTER);
				gl.glPopMatrix();
			}
			gl.glPushMatrix();
			gl.glTranslatef(7, 1, 0);
			gl.glScalef(3, 3, 1);
			gl.glColor4f(0, 0, 0, 1);
			Glyphs.drawString(gl, "" + txt.charAt(txt.length() - 1), null, GameVars.TEXT_ALIGNMENT_CENTER);
			gl.glPopMatrix();
			break;
		case 5: // Snowfall
			break;
		}
		
//		splashImage = new Image(GameVars.TEXTURE_SPLASH_BACKGROUND, Camera.GL_WIDTH, Camera.GL_HEIGHT);
//		gl.glPushMatrix();
//		gl.glTranslatef(Camera.GL_WIDTH * .5f, Camera.GL_HEIGHT * .5f, 0);
//		splashImage.draw(gl);
//		gl.glPopMatrix();
		
//		isReady = true;
	}
	
	// Don't Let Up Stuff
	private void drawBits(GL10 gl){
		drawSquare(gl, squareone);
		drawSquare(gl, squaretwo);
		drawSquare(gl, squarethree);
		drawSquare(gl, squarefour);
		drawSquare(gl, squarefive);
		drawSquare(gl, squaresix);
		drawSquare(gl, squareseven);
		drawSquare(gl, squareeight);
	}
	
	private void drawSquare(GL10 gl, float[] square){
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, VertexBuffer.getVertexBuffer(square));
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, square.length / 3);
		
	}
	
	public boolean next(){ return next; }
//	public boolean isReady(){ return isReady; }
	public boolean isLoaded() {return isLoaded; }

	@Override
	public void buttonHandler(int num) {			
	}

	@Override
	public void handleUp(PointF location) {
		next = true;
	}

	@Override
	public void handleDrag(PointF location) {			
	}

	@Override
	public void handleTouch(PointF location) {			
	}
	
	public boolean handleBackPress(){
		return false;
	}
}
